/** 
 * @Title:  UpdateDialog.java 
 * @author:  Abtswiath丶lxy
 * @data:  2016年6月13日 下午6:20:02 <创建时间>
 * 
 * @history：<以下是历史记录>
 *
 * @modifier: <修改人>
 * @modify date: 2016年6月13日 下午6:20:02 <修改时间>
 * @log: <修改内容>
 *
 * @modifier: <修改人>
 * @modify date: 2016年6月13日 下午6:20:02 <修改时间>
 * @log: <修改内容>
 */
package com.sunmi.usercenter.ui.component;

import java.util.Timer;
import java.util.TimerTask;

import com.sunmi.usercenter.R;
import com.sunmi.usercenter.utils.CharUtils;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * TODO<请描述这个类是干什么的>
 * 
 * @author Abtswiath丶lxy
 * @versionCode 1 <每次修改提交前+1>
 */
public class UpdateDialog extends Dialog {

	private static Context mContext;
	private static UpdateDialog updateDialog = null;
	private static LoadingDialog mLoadingDialog;


	public UpdateDialog(Context context, int theme) {
		super(context, theme);
		this.mContext = context;
	}

	public UpdateDialog(Context context) {
		super(context);
		this.mContext = context;
	}

	public static UpdateDialog createDialog(final Context context, String title, String string,int max,
			final updateInfoLinstener infoLinstener) {
		updateDialog = new UpdateDialog(context, R.style.defaultDialogStyle);
		updateDialog.setContentView(R.layout.dialog_update_edit);
		updateDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
		updateDialog.setCanceledOnTouchOutside(true);
		updateDialog.setCancelable(false);
		mLoadingDialog = LoadingDialog.createDialog(mContext, mContext.getString(R.string.saveing));
		TextView mTxtTitle = (TextView) updateDialog.findViewById(R.id.title);
		final EditText mEditConstant = (EditText) updateDialog.findViewById(R.id.constant);
		mEditConstant.setFilters(new InputFilter[]{new InputFilter.LengthFilter(max)});
		final ImageView imgClear = (ImageView) updateDialog.findViewById(R.id.clear);
		LinearLayout cancel = (LinearLayout) updateDialog.findViewById(R.id.line_cancel);
		RelativeLayout mRelClear = (RelativeLayout) updateDialog.findViewById(R.id.rel_clear);
		LinearLayout cave = (LinearLayout) updateDialog.findViewById(R.id.line_save);
		showInput(mEditConstant);
		mEditConstant.setMaxEms(15);
		cancel.setOnClickListener(new android.view.View.OnClickListener() {

			@Override
			public void onClick(View v) {
				infoLinstener.cancel();
			}
		});
		cave.setOnClickListener(new android.view.View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String msg = mEditConstant.getText().toString();
				if (!CharUtils.isEmpty(msg)) {
					mLoadingDialog.show();
					infoLinstener.save(msg);
				} else {
					Toast.makeText(context, context.getString(R.string.input_dont_null), Toast.LENGTH_SHORT).show();
				}
			}
		});
		
		imgClear.setVisibility(View.GONE);
		imgClear.setOnClickListener(new android.view.View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mEditConstant.setText("");
			}
		});
		mRelClear.setOnClickListener(new android.view.View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mEditConstant.setText("");
			}
			
		});
		
		mEditConstant.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}
			@Override
			public void afterTextChanged(Editable s) {
				if (s.length() > 0) {
					imgClear.setVisibility(View.VISIBLE);
				} else {
					imgClear.setVisibility(View.GONE);
				}
			}
		});
		if(string!=null){
			mEditConstant.setText(string);
		}
		mTxtTitle.setText(title);
		return updateDialog;
	}
	
	private static void showInput(final EditText editText){
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			public void run() {
				InputMethodManager inputManager = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
				inputManager.showSoftInput(editText, 0);
			}
		}, 998);
	}

	public interface updateInfoLinstener {
		public void cancel();
		public void save(String msg);
	}

	@Override
	public void dismiss() {
		super.dismiss();
		if(updateDialog!=null){
			updateDialog = null;
		}
		if(mContext!=null){
			mContext = null;
		}
		if(mLoadingDialog!=null){
			mLoadingDialog.dismiss();
		}
	}
}
